<?php

namespace App\Console\Commands\OpenShop\Nsq;


use App\Enums\Common\NsqEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Service\OpenShop\Feishu\ShopTagsService;
use Illuminate\Console\Command;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Contract\Message;

class NsqSubOrderDeliveryExpressCommand extends Command
{
    protected $signature = 'nsq:sub-delivery-express';

    protected $description = 'NSQ订阅订单发快递事件';

    public function handle()
    {
        (new NsqSubscriber(config('nsq.default.address')))
            ->sub(NsqEnum::TOPIC_KFOS_ORDER_DELIVERY_EXPRESS, NsqEnum::CHANNEL_TIGER_SUB_DELIVERY_EXPRESS,
                function (Message $message) {
                    $this->doHandle($message->data());

                    $message->done();
                });
    }

    private function doHandle(array $data)
    {
        $shopId = $data['shop_id'] ?? 0;
        if (!$shopId) {
            return;
        }

        $orderNo = OpenShopMainProcessModel::query()->where('shopId', $shopId)->value('orderNo');
        if (!$orderNo) {
            return;
        }

        // 更新软装待发货、全部发货标签
        (new ShopTagsService($orderNo))->updateSoftPayDelivery();

        // 更新 设备的支付, 发货状态
        (new ShopTagsService($orderNo))->updateKfosTag();
    }
}
